
import {cityList,village} from "../../http/api"
const app = getApp();


Page({
  data: {
    loading:false,
    pureTown: "西林吉镇", // 新增，存储纯镇名
    selectedCommunity: "",
    cityList: [],
    selectedCity: "西林吉镇",
    check_village: [],
  },
  onLoad(){
    let selectedTown = wx.getStorageSync("selectedTown")
    let community = wx.getStorageSync("community")
    
    // 存储的镇名和村名
    console.log('从存储中获取的镇:', selectedTown);
    console.log('从存储中获取的村:', community);
    
    if(selectedTown !== "" && community !== ""){
      this.setData({
        selectedCommunity: community,
        selectedCity: `${selectedTown}>${community}`,
        pureTown: selectedTown
      });
      
      // 查找存储的镇对应的编号
      // this.city().then(() => {
      //   const townItem = this.data.cityList.find(item => item.Town === selectedTown);
      //   if (townItem) {
      //     console.log('找到存储的镇对应的编号:', townItem.TownNumber);
      //     this.A_village(townItem.TownNumber);
      //   } else {
      //     console.log('未找到存储的镇对应的编号');
      //     // 可以选择默认加载第一个镇的数据
      //     if (this.data.cityList.length > 0) {
      //       this.setData({
      //         pureTown: this.data.cityList[0].Town,
      //         selectedCity: this.data.cityList[0].Town
      //       });
      //       // this.selectCity()
      //       this.A_village(this.data.cityList[0].TownNumber);
      //     }
      //   }
      // });
    } else {
      // 如果没有存储值，正常加载默认数据
      this.city()
      this.A_village(232723100)
    }
    
    console.log('当前选中的城市:', this.data.selectedCity);
  },
  city(){
    cityList({
      Data: 'base_system_town',
      Condition: "CountyNumber LIKE '232723%'",
    }).then(res => {
      console.log(res);
      this.setData({
        cityList: res
      })
    })
  },
  //村的点击事件
  selectCommunity(e) {
    console.log(this.data.selectedCity,'选择的镇');
    console.log(e);
    const selectedTown = this.data.pureTown; // 用纯镇名
    console.log(selectedTown);
    const community = e.currentTarget.dataset.community.Village; //村
    wx.setStorageSync( "community",community)
    console.log(community);
    // 拼接新的显示内容
        const newSelectedCity = `${selectedTown}>${community}`;
    this.setData({
      selectedCommunity: community,
      selectedCity: newSelectedCity
    });
  },
  //镇的点击事件
   selectCity(e) {
    this.setData({
      loading:true
    })
    console.log(this.data.loading);
     console.log(e);
    const selectedTown = e.currentTarget.dataset.name.Town; //镇

    wx.setStorageSync( "selectedTown",selectedTown)
    // wx.setStorageSync(selectedTown,'selectedTown')
    const selectedTownNumber = e.currentTarget.dataset.name.TownNumber //镇编号
    console.log(selectedTownNumber,'选择的城市编号');
    this.setData({
      pureTown: selectedTown ,// 同步更新纯镇名
      selectedCity: selectedTown
    });
    this.A_village(selectedTownNumber)
  },
  A_village(arg_village){
    console.log(arg_village);
    village({
      Data: 'base_system_village',
      Condition: `TownNumber=${arg_village}`,
      // contentType:true
    }).then(res => {
      console.log(res);
      this.setData({
          loading:false,
        check_village:res
      })
    })
  },
  // 切换站点函数
  switchSite() {
    // 此处可添加切换站点的业务逻辑，比如请求接口等
    console.log("切换站点操作，已选择城市：", this.data.selectedCommunity);
    if (this.data.selectedCity == "") {
          wx.showToast({
      title: '请选择镇',
      icon: 'none',  // 不显示图标
      duration: 2000  // 持续2秒
    })
    return
    }
  
    wx.navigateTo({
      url: '../../pages/home/home', //跳转页面
    })
  }
});